# -*- coding:utf-8 -*- 
# Author: json_steve

# 自定义正则转换器
import functools
from werkzeug.routing import BaseConverter
from flask import session,jsonify,g
from iHome.utils.response_code import RET


# 正则转换器
class RegexConverter(BaseConverter):
    def __init__(self, url_map, *args):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]


# 登陆验证装饰器
def login_required(f):
    @functools.wraps(f)  # __name__还是原来的
    def warrper(*args, **kwargs):
        user_id = session.get('user_id')
        if not user_id:
            return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录')
        else:
            g.user_id = user_id  # g对象动态能加属性
            return f(*args, **kwargs)
    return warrper
